package com.sskj.mine.ui.activity;

import android.support.design.widget.BottomSheetDialog;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.hey.lib.HeySpinner;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.util.BottomSheetUtil;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.asset.data.ExchangeConfigBean;
import com.sskj.mine.bean.ExchangeCoinBean;
import com.sskj.mine.presenter.ExchangePresenter;

import java.util.List;

import butterknife.BindView;
import io.reactivex.Flowable;

/**
 * @author Hey
 * Create at  2019/05/04
 */
@Route(path = RConfig.MINE_EXCHANGE)//划转
public class ExchangeActivity extends BaseActivity<ExchangePresenter> {


    @BindView(R2.id.heySpinnerFrom)
    HeySpinner heySpinnerFrom;
    @BindView(R2.id.heySpinnerTo)
    HeySpinner heySpinnerTo;
    @BindView(R2.id.tvCoin)
    TextView tvCoin;
    @BindView(R2.id.etNum)
    EditText etNum;
    @BindView(R2.id.etPwd)
    EditText etPwd;
    @BindView(R2.id.btSure)
    Button btSure;
    @Autowired
    String type;
    private BottomSheetDialog bottomSheetCoin;
    ExchangeConfigBean exchangeConfigBean;
    private BottomSheetDialog bottomSheetWalletType;
    private int toPos;
    private int coinPos;
    private List<ExchangeCoinBean> exchangeCoinBeans;

    @Override
    public int getLayoutId() {
        return R.layout.mine_activity_exchange;
    }

    @Override
    public ExchangePresenter getPresenter() {
        return new ExchangePresenter();
    }

    @Override
    public void initView() {
        ARouter.getInstance().inject(this);
        setTitle(App.INSTANCE.getString(R.string.mine_exchangeActivity1));
        setRightTitle(App.INSTANCE.getString(R.string.mine_exchangeRecordsActivity1), v -> {
            ARouter.getInstance().build(RConfig.MINE_EXCHANGE_RECORD).navigation();
        });
    }

    @Override
    protected void initData() {
        mPresenter.getExchangeWallet(type);
    }

    public void updateWallet(ExchangeConfigBean data) {
        this.exchangeConfigBean = data;
        heySpinnerFrom.setText(data.getFormWallet().getWalletName());
        Flowable.fromIterable(data.getToWallet())
                .map(toWalletBean -> toWalletBean.getWalletName())
                .toList()
                .subscribe((strings, throwable) -> {
                    mPresenter.getExchangeCoin(data.getFormWallet().getWalletType(), data.getToWallet().get(0).getWalletType());
                    heySpinnerTo.setText(strings.get(0));
                    toPos = 0;
                    bottomSheetWalletType = BottomSheetUtil.getBottomSheet(ExchangeActivity.this, null, (recyclerView, i, view) -> {
                        toPos = i;
                        bottomSheetWalletType.dismiss();
                        heySpinnerTo.setText(strings.get(i));
                        mPresenter.getExchangeCoin(data.getFormWallet().getWalletType(), data.getToWallet().get(i).getWalletType());
                    }, strings);
                    ClickUtil.click(heySpinnerTo, () -> {
                        bottomSheetWalletType.show();
                    });
                });
    }

    public void updateCoin(List<ExchangeCoinBean> data) {
        exchangeCoinBeans = data;
        Flowable.fromIterable(data)
                .map(exchangeCoinBean -> exchangeCoinBean.getCode())
                .toList()
                .subscribe((strings, throwable) -> {
                    mPresenter.getUsable(exchangeConfigBean.getFormWallet().getWalletType() + "", data.get(0).getId() + "");
                    tvCoin.setText(data.get(0).getCode());
                    coinPos = 0;
                    bottomSheetCoin = BottomSheetUtil.getBottomSheet(this, null, (recyclerView, i, view) -> {
                        coinPos = i;
                        bottomSheetCoin.dismiss();
                        mPresenter.getUsable(exchangeConfigBean.getFormWallet().getWalletType() + "", data.get(i).getId() + "");
                        tvCoin.setText(data.get(i).getCode());


                    }, strings);
                });
        ClickUtil.click(tvCoin, () -> {
            if (bottomSheetCoin == null) {

            }
            bottomSheetCoin.show();
        });
    }

    String usable;

    public void updateUsable(String usable) {
        this.usable = usable;
        etNum.setHint(App.INSTANCE.getString(R.string.mine_exchangeActivity3)+usable);
        ClickUtil.click(btSure, () -> {
            if (checkParam()) {
                mPresenter.exchange(exchangeConfigBean.getFormWallet().getWalletType() + "",
                        exchangeConfigBean.getToWallet().get(toPos).getWalletType() + "",
                        etNum.getText().toString(), etPwd.getText().toString(),
                        exchangeCoinBeans.get(coinPos).getId() + ""
                );
            }
        });

    }

    private boolean checkParam() {
        if (TextUtils.isEmpty(etNum.getText())) {
            ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_exchangeActivity4));
            return false;
        }
        if (TextUtils.isEmpty(etPwd.getText())) {
            ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_shellActivity5));
            return false;
        }
        if (Double.valueOf(etNum.getText().toString()) > Double.valueOf(usable)) {
            ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_exchangeActivity6));
            return false;
        }

        return true;

    }

    public void exchangeSuccess() {
        ARouter.getInstance().build(RConfig.MINE_EXCHANGE_RECORD).navigation();
        finish();

    }
}
